<?php

namespace app\api\controller\clockin;
use app\api\service\clockin\ClockinActivityService;
use app\BaseController;
use app\api\service\clockin\ClockinActivityUserService;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\api\service\log\AdminOperateLogService;
use app\util\AdminUserUtil;
use app\util\ResponseUtil;
use app\util\UserUtil;
use think\App;

class ClockinActivityUser extends BaseController
{

    private $pageCode = 'page_toolbar';
    private $arrFieldTitle = array("status" => "状态");

    public function __construct(App $app)
    {
        parent::__construct($app);
        $serviceDatagridColumn = new DatagridColumnService();
        $list = $serviceDatagridColumn->getListNoPage(array('page_code' => $this->pageCode, 'status' => 1));
        $this->arrFieldTitle = array_column($list, 'column_title', 'column_name');
    }

    public function getClockinActivityUserInfo()
    {
        $id = $this->requestData['id'];
        $service = new ClockinActivityUserService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getClockinActivityUserList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new ClockinActivityUserService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getClockinActivityUserOption()
    {
        $service = new ClockinActivityUserService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    public function addClockinActivityUser()
    {
        try{
            if(empty($this->requestData['clockin_activity_id'])){
                return ResponseUtil::failure("请指定要加入的打卡活动");
            }
            $clockinActivityId = $this->requestData['clockin_activity_id'];
            $service = new ClockinActivityService();
            $clockinActivityInfo = $service->findRowById($clockinActivityId);
            if(!$clockinActivityInfo){
                return ResponseUtil::failure("打卡活动不存在");
            }
            $addData = array(
                'clockin_activity_id' => $clockinActivityId,
                'user_id' => rand(1,3)
            );
            $service = new ClockinActivityUserService();
            $res = $service->createRow($addData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $this->arrFieldTitle);
                $message = '新增活动用户';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityUser', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("加入打卡活动失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("加入打卡活动失败");
        }
    }

    /**
     * 用户加入打卡活动
     * 前端调用
     * @return \think\response\Json
     */
    public function joinClockinActivity()
    {
        try{
            if(empty($this->requestData['clockin_activity_id'])){
                return ResponseUtil::failure("请指定要加入的打卡活动");
            }
            $clockinActivityId = $this->requestData['clockin_activity_id'];
            $service = new ClockinActivityService();
            $clockinActivityInfo = $service->findRowById($clockinActivityId);
            if(!$clockinActivityInfo){
                return ResponseUtil::failure("打卡活动不存在");
            }
            $addData = array(
                'clockin_activity_id' => $clockinActivityId,
                'user_id' => rand(1,3)
            );
            $service = new ClockinActivityUserService();
            $res = $service->createRow($addData);
            if(0 === $res){
                return ResponseUtil::failure("您已加入当前打卡活动");
            }
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $this->arrFieldTitle);
                $message = '用户加入打卡活动';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description_en' => '',
                    'description_cn' => $message,
                ];
                UserUtil::addOperateLog('joinClockinActivity', 'joinClockinActivity', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("加入打卡活动失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("加入打卡活动失败");
        }
    }

    public function editClockinActivityUser()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的活动用户");
            }

            $id = $this->requestData['id'];
            $service = new ClockinActivityUserService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑活动用户,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityUser', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑活动用户失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑活动用户失败");
        }
    }

    public function deleteClockinActivityUser()
    {
        try{
            $id = $this->requestData['id'];
            $service = new ClockinActivityUserService();
            $info = $service->findRowById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->removeRowById($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($info, $this->arrFieldTitle);
                $message = '删除活动用户';
                if($deleteInfo){
                    $message .= ','.$deleteInfo;
                }
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityUser', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除活动用户失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除活动用户失败");
        }
    }

    public function deleteMultiClockinActivityUser(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new ClockinActivityUserService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->findRowById($value);
                if(!$info){
                    return ResponseUtil::failure("活动用户不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->removeListByManyId($id);
            if($res){
                $message = '删除活动用户';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityUser', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除活动用户失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除活动用户失败");
        }
    }

    public function simpleUpdateClockinActivityUser()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的活动用户");
            }

            $id = $this->requestData['id'];
            $service = new ClockinActivityUserService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑活动用户,'.$changeInfo['update_info'];
                $logData = [
                    'subject' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityUser', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑活动用户失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑活动用户失败");
        }
    }

}



